package com.situ.web.util;

import java.sql.*;

//JDBC工具类
public class JDBCUtil {

    private JDBCUtil(){
    }
    //静态代码块 保证只执行加载一次驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //建立与数据库的连接
    public static Connection getConnnection() throws SQLException {
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/java1?useUnicode=true&characterEncoding=UTF-8", "root", "root");
        return connection;
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet){
        //先打开的后关闭
        if(resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
